package com.mapapp.flutter_mapapp.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.activity.NewDataActivity;
import com.mapapp.flutter_mapapp.adapter.StepDetailAdapter;
import com.mapapp.flutter_mapapp.adapter.StepListAdapter;
import com.mapapp.flutter_mapapp.adapter.StepListDialogAdapter;
import com.mapapp.flutter_mapapp.bean.DetailTree;
import com.mapapp.flutter_mapapp.bean.StepFloorBean;
import com.mapapp.flutter_mapapp.bean.StepFloorProcessBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StepFloorDetailDialog extends Dialog {
    private static final String TAG = "StepFloorDetailDialog";
    @BindView(R.id.iv_dialog_step_close)
    ImageView ivDialogStepClose;
    @BindView(R.id.rv_dialog_step_floor)
    RecyclerView rvDialogStepFloor;
    @BindView(R.id.tv_dialog_step_address)
    TextView tvDialogStepAddress;
    @BindView(R.id.rv_dialog_step_detail)
    RecyclerView rvDialogStepDetail;
    @BindView(R.id.tv_dialog_step_examine)
    Button tvDialogStepExamine;

    private Context mContext;
    private StepFloorBean mStepBeanAll;
    private StepListDialogAdapter mFloorAdapter;
    private StepDetailAdapter mDetailAdapter;
    private int mPosition = 0;

    public StepFloorDetailDialog(Context context) {
        super(context, R.style.pickPhotoDialog);
        mContext = context;
        setCancelable(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_step_floor_detail);
        ButterKnife.bind(this);

        ivDialogStepClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvDialogStepExamine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mStepBeanAll.getRegions().get(mPosition).getFixNoAuditNum() > 0){
                    Toast.makeText(mContext,"当前有未审核数据，请等待审核。",Toast.LENGTH_SHORT).show();
                }else if(mStepBeanAll.getRegions().get(mPosition).getFixNoAuditNum() == 0){
                    Intent intent = new Intent(mContext, NewDataActivity.class);
                    intent.putExtra("regionId", mStepBeanAll.getRegions().get(mPosition).getRegionId());
                    intent.putExtra("type", 0);
                    intent.putExtra("householdName", mStepBeanAll.getRegions().get(mPosition).getRegionName());
                    intent.putExtra("address", mStepBeanAll.getRegions().get(mPosition).getAddress());
                    intent.putExtra("task_id", mStepBeanAll.getRegions().get(mPosition).getTaskId());
                    mContext.startActivity(intent);
                    dismiss();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void setmStepBean(StepFloorBean mStepBean) {
        this.mStepBeanAll = mStepBean;
        mPosition = 0;
        showInfo();
    }

    private void showInfo() {
        if (mStepBeanAll != null) {
            mFloorAdapter = new StepListDialogAdapter(mStepBeanAll,mContext);
            mFloorAdapter.setOnItemClickListener(new StepListDialogAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    mPosition = position;
                    ArrayList<StepFloorProcessBean> floors = mStepBeanAll.getRegions().get(mPosition).getProcesses();
                    mDetailAdapter.notify(floors);
                    tvDialogStepAddress.setText(mStepBeanAll.getRegions().get(mPosition).getAddress());
                }
            });
            LinearLayoutManager llmanage = new LinearLayoutManager(mContext);
            llmanage.setOrientation(LinearLayoutManager.HORIZONTAL);
            rvDialogStepFloor.setLayoutManager(llmanage);
            rvDialogStepFloor.setAdapter(mFloorAdapter);

            tvDialogStepAddress.setText(mStepBeanAll.getRegions().get(mPosition).getAddress());

            ArrayList<StepFloorProcessBean> floors = mStepBeanAll.getRegions().get(mPosition).getProcesses();
            mDetailAdapter = new StepDetailAdapter(floors,mContext);
            LinearLayoutManager llmanage1 = new LinearLayoutManager(mContext);
            llmanage1.setOrientation(LinearLayoutManager.VERTICAL);
            rvDialogStepDetail.setLayoutManager(llmanage1);
            rvDialogStepDetail.setAdapter(mDetailAdapter);
        }
    }
}
